<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="erpName">
        <el-input
          v-model="queryParams.erpName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="chinaName">
        <el-input
          v-model="queryParams.chinaName"
          placeholder="请输规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="partId">
        <el-input
          v-model="queryParams.partId"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="物料类型" prop="mateType">
        <el-select v-model="queryParams.mateType" placeholder="请选择物料类型" clearable>
          <el-option
            v-for="dict in dict.type.mate_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="期间" prop="periodDate">
        <el-date-picker
          :clearable="false"
          v-model="queryParams.periodDate"
          type="month"
          value-format="yyyy-MM"
          placeholder="请选择期间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:matebom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:matebom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:matebom:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:matebom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="matebomList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
        <el-table-column label="物料名称" align="center" prop="erpName" />
      <el-table-column label="规格" align="center" prop="chinaName" />
      <el-table-column label="物料编码" align="center" prop="partId" />
      <el-table-column label="物料类型" align="center" prop="mateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mate_type" :value="scope.row.mateType"/>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit" />
      <!-- <el-table-column label="数量类型" align="center" prop="numType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.num_type" :value="scope.row.numType"/>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="消耗数量" align="center" prop="mateNum" />
      <el-table-column label="销售数量" align="center" prop="proNum" /> -->
      <el-table-column label="数量" align="center" prop="counts" />
      <el-table-column label="期间" align="center" prop="periodDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.periodDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:matebom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:matebom:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料销售数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型编码" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型编码">
            <el-option
              v-for="dict in dict.type.mtr_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称" prop="chinaName">
          <el-input v-model="form.chinaName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="期间时间" prop="periodDate">
          <el-date-picker clearable
            v-model="form.periodDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择期间时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物料类型" prop="mateType">
          <el-select v-model="form.mateType" placeholder="请选择物料类型">
            <el-option
              v-for="dict in dict.type.mate_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量类型" prop="numType">
          <el-select v-model="form.numType" placeholder="请选择数量类型">
            <el-option
              v-for="dict in dict.type.num_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消耗数量" prop="mateNum">
          <el-input v-model="form.mateNum" placeholder="请输入消耗数量" />
        </el-form-item>
        <el-form-item label="销售数量" prop="proNum">
          <el-input v-model="form.proNum" placeholder="请输入销售数量" />
        </el-form-item>
        <el-form-item label="合计数量" prop="totalNum">
          <el-input v-model="form.totalNum" placeholder="请输入合计数量" />
        </el-form-item>
        <el-form-item label="物料名称" prop="erpName">
          <el-input v-model="form.erpName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="别名" prop="asName">
          <el-input v-model="form.asName" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="bom名称" prop="bomName">
          <el-input v-model="form.bomName" placeholder="请输入bom名称" />
        </el-form-item>
        <el-form-item label="bom版本" prop="bomVar">
          <el-input v-model="form.bomVar" placeholder="请输入bom版本" />
        </el-form-item>
        <el-form-item label="父项编码" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父项编码" />
        </el-form-item>
        <el-form-item label="父项目版本" prop="fatherPartvar">
          <el-input v-model="form.fatherPartvar" placeholder="请输入父项目版本" />
        </el-form-item>
        <el-form-item label="物料编码" prop="partId">
          <el-input v-model="form.partId" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="期间" prop="month">
          <el-input v-model="form.month" placeholder="请输入期间" />
        </el-form-item>
        <el-form-item label="层次号" prop="assembLelevel">
          <el-input v-model="form.assembLelevel" placeholder="请输入层次号" />
        </el-form-item>
        <el-form-item label="物料版本" prop="partVar">
          <el-input v-model="form.partVar" placeholder="请输入物料版本" />
        </el-form-item>
        <el-form-item label="数量" prop="counts">
          <el-input v-model="form.counts" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="年度" prop="year">
          <el-input v-model="form.year" placeholder="请输入年度" />
        </el-form-item>
        <el-form-item label="备注" prop="marks">
          <el-input v-model="form.marks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="基期" prop="partDate">
          <el-date-picker clearable
            v-model="form.partDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择基期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="root值" prop="rootCode">
          <el-input v-model="form.rootCode" placeholder="请输入root值" />
        </el-form-item>
        <el-form-item label="序列号" prop="seqnum">
          <el-input v-model="form.seqnum" placeholder="请输入序列号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMatebom, getMatebom, delMatebom, addMatebom, updateMatebom } from "@/api/factory/materialxs/matebom";

export default {
  name: "Matebom",
  dicts: ['mate_type', 'mtr_type', 'num_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料销售数据表格数据
      matebomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chinaName: null,
        periodDate: null,
        mateType: null,
        erpName: null,
        partId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chinaName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        partId: [
          { required: true, message: "物料编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getNowTime();
    this.getList();
  },
  methods: {
    getNowTime() {
       var now = new Date();
       var year = now.getFullYear(); //得到年份
       var month = now.getMonth(); //得到月份
       var date = now.getDate(); //得到日期
      //  month = month + 1;
       month = month.toString().padStart(2, "0");
       date = date.toString().padStart(2, "0");
       var defaultDate = `${year}-${month}`;
      //  this.queryParams.periodDate=defaultDate
       this.$set(this.queryParams, "periodDate", defaultDate);
      //  return  defaultDate
      //  this.$set(this.queryParams, "periodDate", defaultDate);
   },
    /** 查询物料销售数据列表 */
    getList() {
      this.loading = true;
      listMatebom(this.queryParams).then(response => {
        this.matebomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        chinaName: null,
        periodDate: null,
        mateType: null,
        numType: null,
        mateNum: null,
        proNum: null,
        totalNum: null,
        erpName: null,
        asName: null,
        bomName: null,
        bomVar: null,
        parentId: null,
        fatherPartvar: null,
        partId: null,
        month: null,
        assembLelevel: null,
        partVar: null,
        counts: null,
        year: null,
        marks: null,
        createTime: null,
        updateTime: null,
        partDate: null,
        rootCode: null,
        seqnum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料销售数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMatebom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料销售数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMatebom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMatebom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料销售数据编号为"' + ids + '"的数据项？').then(function() {
        return delMatebom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/matebom/export', {
        ...this.queryParams
      }, `matebom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
